using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CreamX.Framework.Game.Components;
using Microsoft.Xna.Framework.Graphics;
using CreamX.Framework.Game;
using CreamX.Framework;


namespace CreamXStudio.XNARenderer
{
    public partial class TileSetRenderer : GraphicsDeviceControl
    {
        internal class Cell
        {
            public override bool Equals(object obj)
            {
                if (obj == null)
                    return false;
                Cell c=(Cell)obj;
                return c.Rect.Equals(Rect);
            }
            public bool Highlighted;
            public Microsoft.Xna.Framework.Rectangle Rect=Microsoft.Xna.Framework.Rectangle.Empty;
        }
       internal Cell HighlightedCell;
        internal List<Cell> Cells = new List<Cell>();
        public TileSetRenderer()
        {

        }
        public CreamXTileGridComponent TileGrid;
        PrimitiveBatch primitiveBatch;
        SpriteBatch sprite;
        protected override void Initialize()
        {
            primitiveBatch = new PrimitiveBatch(GraphicsDevice);
            Application.Idle += delegate { Invalidate(); };
            sprite = new SpriteBatch(GraphicsDevice);
        }
        bool init = false;
        protected override void Draw()
        {
            
            GraphicsDevice.Clear(Microsoft.Xna.Framework.Graphics.Color.CornflowerBlue);
            //Draw Grid
           
            if (TileGrid != null)
            {
                if (!init)
                    InitGrid();
           
                sprite.Begin();
                bool __ts = TileGrid.SceneObject.IgnoreCameraPosition;
                TileGrid.SceneObject.IgnoreCameraPosition = true;
                TileGrid.SceneObject.SetPosition(0, 0);
                ((CreamXTileGrid)TileGrid.SceneObject).Draw(sprite);
                TileGrid.SceneObject.IgnoreCameraPosition = __ts;
                sprite.End();
                DrawGrid();
           
            }
        }

        private void InitGrid()
        {
            Cells.Clear();
            init = true;
             int rows = TileGrid.TileCountY;
            int cols = TileGrid.TileCountX;
            for (int i = 0; i < rows; i++)
            {
                for (int x = 0; x < cols; x++)
                {
                    Cell c = new Cell();
                    Microsoft.Xna.Framework.Rectangle r = new Microsoft.Xna.Framework.Rectangle((int)(
                           ( x * TileGrid.TileDisplaySizeX) * TileGrid.SceneObject.Scene.Camera.Zoom),
                           (int)((i * TileGrid.TileDisplaySizeY) * TileGrid.SceneObject.Scene.Camera.Zoom),
                           (int)(TileGrid.TileDisplaySizeX * TileGrid.SceneObject.Scene.Camera.Zoom),
                           (int)(TileGrid.TileDisplaySizeY * TileGrid.SceneObject.Scene.Camera.Zoom));
                    c.Rect = r;
                    Cells.Add(c);
                }
            }
        }

        private void DrawGrid()
        {
            InitGrid();
            foreach (Cell c in Cells)
            {
                Microsoft.Xna.Framework.Vector2 tleft = new Microsoft.Xna.Framework.Vector2(c.Rect.X, c.Rect.Y);
                Microsoft.Xna.Framework.Vector2 bright = new Microsoft.Xna.Framework.Vector2(c.Rect.X + c.Rect.Width, c.Rect.Y + c.Rect.Height);
                primitiveBatch.DrawBox(tleft, 
                            bright,Microsoft.Xna.Framework.Graphics.Color.Blue);
                if (c.Highlighted)
                {
                    
                }
            }
            if (HighlightedCell != null)
            {
                Microsoft.Xna.Framework.Vector2 tleft = new Microsoft.Xna.Framework.Vector2(HighlightedCell.Rect.X, HighlightedCell.Rect.Y);
                Microsoft.Xna.Framework.Vector2 bright = new Microsoft.Xna.Framework.Vector2(HighlightedCell.Rect.X + HighlightedCell.Rect.Width,
                                 HighlightedCell.Rect.Y + HighlightedCell.Rect.Height);
                primitiveBatch.DrawBox(tleft,
                                 bright, 
                                 Microsoft.Xna.Framework.Graphics.Color.Red);
            }
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            HighlightedCell = null;
            foreach (Cell c in Cells)
            {
                if (ContainsPoint(c.Rect, new Microsoft.Xna.Framework.Point(e.X, e.Y)))
                {
                    c.Highlighted = true;
                    HighlightedCell = c;
                }
                else
                    c.Highlighted = false;
            }
            base.OnMouseMove(e);
        }

        private bool ContainsPoint(Microsoft.Xna.Framework.Rectangle rect, Microsoft.Xna.Framework.Point point)
        {
            if (rect.Contains(point))
                return true;
            else
                return false;
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {

            base.OnMouseDown(e);
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
        }

    }





}
